/*
    The MIT License

    Copyright (c) 2010 IFMO/GameDev Studio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#pragma once

/*-----------------------------------------------------------------------------
	forward rendering scene :
	IPxGVScene
-----------------------------------------------------------------------------*/

typedef WeakRef<IFREntity>	IWrFREntity;
typedef WeakRef<IFRLight>	IWrFRLight;

class EFRScene : public IFRScene {	
	public:
							EFRScene		( ERenderSystem *rs );
							~EFRScene		( void );

		virtual void		SetGlobalFXTime	( float seconds )	{ global_fx_time = seconds; }
		virtual float		GetGlobalFXTIme	( void ) const		{ return global_fx_time ;	}
	
		virtual void		ReloadShaders	( void );
		virtual void		ReloadResources	( void );
		virtual void		RenderScene		( void );

		virtual void		SetView			( const EPoint &position, const EQuaternion &orient );
		virtual void		SetProjection	( float znear, float zfar, float width, float height );

		virtual IPxFREntity	AddEntity		( void );
		virtual	IPxFRLight	AddLight		( void );
		virtual IPxFRGFX	AddGFX			( void );
		
		virtual void		SetAmbientLevel	( const EColor &ambient_level );

		virtual void		GetViewProj		( EFrustum &view_frustum, EPoint &view_pos, EQuaternion &view_orient );

		virtual void		AddWater			( const EString &mesh, const EString &noise, float omega, float scale, float falloff );
		virtual void		SetWaterTime		( float time );
		virtual void		RemoveWater			( void );
		virtual void		GetWaterDynamics	( const EPoint &pos, float &pressure, float &surface_level, EVector &velocity );
		virtual void		MakeWaterDrop		( const EPoint &pos, float offset );

		virtual	IPxFRVolume	AddVolume		( void );
		virtual void		RemoveVolume	( void );

	public:
		int					DefineShader	( lua_State *L );
		
		void				RenderShadowMap	( IPxTexture shadow_map, const EMatrix &view, const EMatrix &proj );
		
	private:
		float global_fx_time;
		
	public:
		ERenderSystem		*rs;
		HardRef<class EFRShadingSystem>		sshs;
		HardRef<class EFRLightingSystem>	lits;
		HardRef<class EFRMlaa>				mlaa;
		HardRef<class EFRWater>				water;
		HardRef<class EFRScreenFX>			scrfx;
		HardRef<class EFRGFXSystem>			gfxsys;
		HardRef<class EFRSsao>				ssao;
		HardRef<class EFRVolume>			volume;

		IPxRSDriver		driver;		
		IPxFXManager	fxm;
		IPxVBManager	vbm;
		IPxTXManager	txm;
		IPxRTManager	rtm;

	protected:
		void RenderSceneInternal ( IPxTexture color, IPxTexture depth, int subframe );
	
		void				RenderShadowMap		( IPxTexture shadow_map );
		void				RenderSolid			( EGBuffer &gbuf );
		void				RenderTranslucent	( void );
		void				RenderEmission		( void );
		void				RenderDistortion	( void );
		void				RenderWireframe		( void );
		
		void				RenderLighting		( EGBuffer &gbuf, IPxTexture color, IPxTexture depth );
		void				RenderMlaa			( IPxTexture color, IPxTexture depth );
		void				RenderSsao			( IPxTexture depth, IPxTexture normals, IPxTexture color );
		
	public:		
		
		struct {
			float	random;
			float	time;
		} common;
	
		struct {
			EPoint			position;
			EQuaternion		orient;
			EMatrix			matrix_view;
			EFrustum		frustum;
		} view;
		
		struct {
			EMatrix	matrix_proj;
		} proj;
		
		struct {
			EColor	color;
		} ambient;
		
		std::vector<IWrFREntity>	entities;
		std::vector<IWrFRLight>	lights;
		
		IPxVar	fr_wireframe;
	};